package com.example.login1;

import androidx.appcompat.app.AppCompatActivity;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class MainActivity extends AppCompatActivity {
    public static final String host="http://124.93.196.45:10001";

    private EditText username;
    private EditText password;
    private Button Btn;
    private String username1;
    private String password1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        Btn = (Button) findViewById(R.id.Btn);
        Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                longUser();
            }
        });
    }
    public void longUser(){
        username1 = username.getText().toString();
        password1 = password.getText().toString();
        retrofitLogin();
    }
    public void retrofitLogin(){
        LoginRequest loginRequest = new LoginRequest(username1, password1);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(host)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiServiers apiServiers = retrofit.create(ApiServiers.class);
        apiServiers.postLogin(loginRequest).enqueue(new Callback<GsonBean>() {
            @Override
            public void onResponse(Call<GsonBean> call, Response<GsonBean> response) {
                System.out.println(response.body());
                if(response.isSuccessful()){
                    GsonBean gsonBean = response.body();
                    if (gsonBean != null){
                    String  jsonResult = gsonBean.toString();
                    System.out.println(jsonResult);
                    String msg = gsonBean.msg;
                    int code = Integer.parseInt(gsonBean.code);
                    String token = gsonBean.token;
                    getSharedPreferences("user",MODE_PRIVATE).edit().putString("token",response.body().token).commit();
                    String string = getSharedPreferences("user",MODE_PRIVATE).getString("token","");
                    System.out.println("储存的token"+string);

                    System.out.println("Msg:"+msg);
                    System.out.println("Code:"+code);
                    System.out.println("Token:"+token);
                }else {
                        Log.e("新闻信息", "响应数据为空");
                    }
                }else {
                    Log.e("新闻信息","请求失败，错误码" + response.code());
                }
            }

            @Override
            public void onFailure(Call<GsonBean> call, Throwable throwable) {
                Log.e("新闻信息","请求失败，错误信息" + throwable.getMessage());
            }
        });
    }
}